package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.common.exception.CommonException;
import cn.zysuyuan.item.entity.Permission;
import cn.zysuyuan.item.service.PermissionService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-17 11:06
 */

@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 保存
     */
    @PostMapping("/permission")
    public R save(@RequestBody Map<String,Object> map) throws Exception {
        permissionService.save(map);
        return R.ok();
    }

    /**
     * 查询列表
     */
    @GetMapping("/permission")
    public R findAll(@RequestParam Map<String,Object> map) {
        List<Permission> list = permissionService.findAll(map);
        Map<String,Object> maps = new HashMap<>();
        maps.put("data",list);
        return R.ok(maps);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/permission/{id}")
    public R findById(@PathVariable("id") String id ) {
        Map<String,Object> map = permissionService.findById(id);
        return R.ok(map);
    }

    /**
     * 修改
     */
    @PutMapping("/permission/{id}")
    public R update(@PathVariable("id") String id,@RequestBody Map<String,Object> map ) throws Exception {
        map.put("id",id);
        permissionService.update(map);
        return R.ok();
    }

    /**
     * 根据id删除
     */
    @DeleteMapping("/permission/{id}")
    public R delete(@PathVariable("id") String id) throws CommonException {
        permissionService.deleteById(id);
        return R.ok();
    }

}
